package org.afs.gescol.core.dao.implementations;

import java.util.List;

import org.afs.gescol.core.dao.interfaces.SeriesDao;
import org.afs.gescol.core.dto.SeriesCriteria;
import org.afs.gescol.core.model.references.Series;
import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.scub.foundation.framework.core.dao.implementations.HibernateDaoBaseImplementation;

/**
 * Implementation of SeriesDao.
 * @autor Kobla GBENYO (kobla.gbenyo@gmail.com)
 */
public class SeriesDaoImpl extends HibernateDaoBaseImplementation implements SeriesDao {

    @Override
    public Series getSeriesById(Long id) {
        return load(id, Series.class);
    }

    @SuppressWarnings("unchecked")
    @Override
    public List<Series> getSeriesByCriteria(SeriesCriteria seriesCriteria) {
        final Criteria criteria = createCriteria(Series.class);
        if (seriesCriteria.getLabel() != null && !"".equals(seriesCriteria.getLabel())) {
            criteria.add(Restrictions.or(
                Restrictions.ilike("shortLabel", seriesCriteria.getLabel(), MatchMode.ANYWHERE),
                Restrictions.ilike("longLabel", seriesCriteria.getLabel(), MatchMode.ANYWHERE)
            ));
        }
        if (seriesCriteria.getCycleId() != null) {
            criteria.add(Restrictions.eq("seriesCycle.id", seriesCriteria.getCycleId()));
        }
        if (seriesCriteria.getLevelId() != null) {
            criteria.add(Restrictions.eq("seriesLevel.id", seriesCriteria.getLevelId()));
        }
        criteria.addOrder(Order.asc("shortLabel"));
        return criteria.list();
    }

}
